# -*- coding: utf-8 -*-
import requests
import json
from hashlib import md5
import base64
import hmac
from hashlib import sha256

host = "https://salary.eapps.dingtalkcloud.com"
appId = "8130749"
appSecret = "Goau4Jr9gkvG61xnZx2vvv_HPptbaJ61u9G7ZT6TFbgOSZVcH2HaAG95gNv2DJgI"


def main():
    # uri = "/oapi/salary/floatData/items"
    uri = "/oapi/salary/floatData/items"
    url = host + uri

    params = {
        'salaryGroupName': '测试薪资组',
        'calBizId': "61331038"
    }

    params_json = json.dumps(params, ensure_ascii=False, separators=(',', ':'))

    params_encoding = bytes(params_json, encoding='utf-8')

    # md5
    f = md5()
    f.update(params_encoding)

    param_base64 = base64.b64encode(f.digest())
    method = "POST"
    sign_content = method + "\n" + str(param_base64, 'utf-8') + "\n" + uri

    secret_encode = appSecret.encode(encoding="utf-8")
    sign_content_encode = sign_content.encode(encoding="utf-8")
    hash_content = hmac.new(secret_encode, sign_content_encode, digestmod=sha256).digest()
    signature_encode = base64.encodebytes(hash_content)
    signature = str(signature_encode, 'utf-8').replace("\n", "")

    headers = {
        'content-type': "application/json",
        'app-id': appId,
        'signature': signature
    }

    rsp = requests.post(url, data=params_encoding, headers=headers)
    print(rsp.text)


main()
